package com.rop.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rop.model.system.SysRole;
import com.rop.model.system.SysUserRole;
import com.rop.model.vo.AssignRoleVo;
import com.rop.model.vo.SysRoleQueryVo;
import com.rop.system.mapper.SysRoleMapper;
import com.rop.system.mapper.SysUserRoleMapper;
import com.rop.system.service.SysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Title: SysRoleServiceImpl
 * @Author Rop
 * @Date 2023/4/6 21:23
 * @description: 角色业务层
 */

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 根据角色id查询角色详情
     *
     * @param id 角色id
     * @return 详情
     */
    @Override
    public SysRole getInfoById(Long id) {
        return sysRoleMapper.getInfoById(id);
    }

    /**
     * 一键恢复删除的角色
     *
     * @return 是否恢复成功
     */
    @Override
    public Boolean recover() {
        int rows = sysRoleMapper.recover();
        if (rows > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    /**
     * 条件分页查询
     *
     * @param pageParam      分页参数
     * @param sysRoleQueryVo 查询条件
     * @return 查询结果
     */
    @Override
    public IPage<SysRole> getList(Page<SysRole> pageParam, SysRoleQueryVo sysRoleQueryVo) {
        return baseMapper.getList(pageParam, sysRoleQueryVo);
    }

    /**
     * 获取用户角色数据
     * @param userId 用户id
     * @return 结果集
     */
    @Override
    public Map<String, Object> getUserRoleById(Long userId) {
        // 所有角色
        List<SysRole> sysRoleList = baseMapper.selectList(null);

        // 根据用户id查询已分配角色
        QueryWrapper<SysUserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<SysUserRole> sysUserRoleList = sysUserRoleMapper.selectList(wrapper);
        List<String> roleList = sysUserRoleList.stream()
                .map(SysUserRole::getRoleId)
                .filter(StringUtils::isNoneBlank)
                .collect(Collectors.toList());

        Map<String, Object> map = new HashMap<>();
        // 所有角色
        map.put("allRoles", sysRoleList);
        // 用户id已分配角色
        map.put("userRoleIds", roleList);
        return map;
    }

    /**
     * 用户分配角色
     * 采取先删后增
     * @param assignRoleVo 分配角色
     * @return 分配结果
     */
    @Override
    public boolean doAssign(AssignRoleVo assignRoleVo) {
        // 根据用户id删除已分配角色
        QueryWrapper<SysUserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", assignRoleVo.getUserId());
        sysUserRoleMapper.delete(wrapper);

        // 获取所有角色id，添加角色用户关系
        Integer rows = 0;
        if (CollUtil.isNotEmpty(assignRoleVo.getRoleIdList())) {
            rows = sysUserRoleMapper.batchInsert(assignRoleVo);
        }
        return rows < 0 ? Boolean.FALSE : Boolean.TRUE;
    }
}
